<?php

namespace App\Http\Controllers\home;

use App\Model\book;
use App\Model\bookroll;
use App\Model\category;
use App\Model\chapter;
use App\Model\chat;
use App\Model\coller;
use App\Model\comment;
use App\Model\commentsgood;
use App\Model\order;
use App\Model\piddetail;
use App\Model\presse;
use App\Model\readuser;
use App\Model\readuserboxe;
use App\Model\record;
use App\Model\scangood;
use App\Model\user;
use App\Model\writer;
use App\Model\writer_take;
use App\Tool\SMS\SendTemplateSMS;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class HomeController extends Controller
{
    //主页
    public function index()
    {
//        $books=book::all();
        $bbt_img = readuserboxe::all();
        $books = DB::table('books')->skip(0)->take(8)->get();  //热门
        $newbooks=DB::table('books')->skip(0)->take(8)->orderby('addtime','desc')->get(); //新书
        $dibooks=DB::table('books')->skip(0)->take(8)->where('price','>','0')->orderby('price','desc')->get(); //低价
        $mianbooks=DB::table('books')->skip(0)->take(8)->where('free','all')->get(); //低价
        $sum=DB::table('books')->where('free','all')->get();
        $sum=count($sum);
        $a=array();
        foreach ($bbt_img as $k){
            $a[]=$k->iconname;
        }
        $pre=presse::all(); //出版社
        return view('linfeng.home.index',compact('books','a','newbooks','dibooks','mianbooks','sum','pre'));
    }

    //搜索
    public function search(Request $request)
    {

        $search=book::where('bookname',$request->bookname)->get();
//        dd($_GET['bookname']);
        if (count($search)){
            return json_encode($search);
        }else{
            return 111;
        }
    }

    //主页下面的出版社选择 ajax
    public function indexpress(Request $request)
    {
//        $indexpre=presse::find($request->pid);
        $indexpre=DB::table('presses')->where('id',$request->pid)->get();
        echo json_encode($indexpre);
    }

    //raaduser注册
    public function logon(Request $request)
    {
        if ($request->isMethod('post')){

            $rules=array(
                'nickname'=>'required|unique:readusers,nickname',
                'password'=>'required|between:6,10|confirmed',
                'email'=>'required|email|unique:readusers,email',
                'tel'=>'required|unique:readusers,tel',
            );
            $mess=array(
                'nickname.required'=>'用户名不能为空',
                'email.required'=>'邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
                'tel.required'=>'手机号码不能为空',
                'password.required'=>'密码不能为空',
                'password.between'=>'密码必须在:mix和max之间',
                'password.confirmed'=>'密码必须相同',
                'email.unique'=>'邮箱已被注册',
                'tel.unique'=>'号码已被注册',
                'nickname.unique'=>'昵称已被注册',
            );
            $this->validate($request,$rules,$mess);
            //验证码验证
            if (session('code')==$request->code){
                $request->session()->forget('code');
                $add=new readuser();
//                $hash=$request->password()->fill(['password'=>Hash::make($request->password)])->save();
                $has=Hash::make($request->password);
//                dd($hash);

                $add->nickname=$request->nickname;
                $add->tel=$request->tel;
                $add->email=$request->email;
                $add->pwd=$has;
                $add->regtime=time();
                $add->save();

                return redirect('/home/login');

            } else{
                $docode='验证码不正确';
                return view('linfeng.home.logon',compact('docode'));
            }
//        }else{
//            $docode='';
//            return view('linfeng.home.logon',compact('docode'));
        }

        $docode='';
        return view('linfeng.home.logon',compact('docode'));
    }

    //登入发送手机验证码
    public function sendSMS(Request $request)
    {
        $tel=$_GET['tel'];
        $add=readuser::where('tel',$tel)->get();
        if ( count($add)){
            $rand=rand(1000,9999);
            session(['code'=>$rand]);
            $sms=new SendTemplateSMS();

            $sms->sendSMS($tel,array($rand,5),1);

            return 111;
        }else{
            return 222;

        }
    }

    //账户登入
    public function zhanghu(Request $request)
    {
        $rules=array(
            'nickname'=>'required',
            'password'=>'required',
        );
        $mess=array(
            'nickname.required'=>'昵称不能为空',
            'password.required'=>'密码不能为空',
        );
        $this->validate($request,$rules,$mess);

            $nickname=$request->nickname;
            $password=$request->password;

            $see=readuser::where('nickname',$nickname)->get();
                foreach ($see as $k){
                    $readid=$k->id;
                    $pwd=$k->pwd;
                }

            if (count($see)){
                if (!Hash::check($password,$pwd)){
                    $docode='密码不正确';
                    return view('linfeng.home.login',compact('docode'));
                }
                $readname=readuser::where('nickname',$nickname)->get();
                foreach ($readname as $k){
                    $readid=$k->id;
                    $readname=$k->nickname;
                    $backgroud=$k->backgroud;
                }
//                $readchapters=piddetail::where('readid',$readid)->get();
                //吧用户信息放入session
                session(['readtel'=>$request->tel]);
                session(['readid'=>$readid]);
                session(['readname'=>$readname]);
                return redirect('home/index');
            }else{
            $docode='账户不正确';
            return view('linfeng.home.login',compact('docode'));
        }

    }

    //注册发送手机验证码
    public function logonsendSMS(Request $request)
    {
        $tel=$_GET['tel'];
        session(['tel'=>$tel]);
        $rand=rand(1000,9999);
        session(['code'=>$rand]);
        $sms=new SendTemplateSMS();

        $sms->sendSMS($tel,array($rand,5),1);
        return 333;
    }

    //用户登入
    public function login(Request $request)
    {
        if ($request->isMethod('post')){
            $rules=array(
                'password'=>'required',
                'tel'=>'required',
                'code'=>'required'
            );
            $mess=array(
                'tel.required'=>'手机号码不能为空',
                'password.required'=>'密码不能为空',
                'code.required'=>'短信验证码不能为空',
            );
            $this->validate($request,$rules,$mess);
            if (session('code')==$request->code){
                $tel=$request->tel;
                $password=$request->password;

                $see=readuser::where('tel',$tel)->get();
                foreach ($see as $k){
                    $readid=$k->id;
                    $pwd=$k->pwd;
                }
                if (count($see)){

                    if (!Hash::check($password,$pwd)){
                        $docode='密码不正确';
                        return view('linfeng.home.login',compact('docode'));
                    }
                    $readname=readuser::where('tel',$request->tel)->get();
                    foreach ($readname as $k){
                        $readid=$k->id;
                        $readname=$k->nickname;
                        $backgroud=$k->backgroud;
                    }
                    $readchapters=piddetail::where('readid',$readid)->get();
                    //吧用户信息放入session
                    session(['readtel'=>$request->tel]);
                    session(['readid'=>$readid]);
                    session(['readname'=>$readname]);
                    return redirect('home/index');
                }else{
                    $docode='手机号码不存在';
                    return view('linfeng.home.login',compact('docode'));
                }
            }else{
                $docode='验证码不正确';
                return view('linfeng.home.login',compact('docode'));
            }
        }
        $docode='';
        return view('linfeng.home.login',compact('docode'));
    }

    //分类
    public function sort($id=0,$do=0)
    {
        //id是分类名
        //$do是操作名

        //先判断有没有分类
        if ($id){
            //在有分类中判断有没有操作
            if ($do){
                switch ($do){
                    case "scan":
                        $sort=category::all();
                        $books=DB::table('books')->where('cid',$id)->orderby($do,'asc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                    case "netime":
                        $sort=category::all();
                        $santime=time()-2592000;
                        dump(date('Y-m-d',$santime));
                        $books=DB::table('books')->where('cid',$id)->where('addtime','>',$santime)->orderby('addtime','desc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                    case "buy":
                        $sort=category::all();
                        $books=DB::table('books')->where('cid',$id)->orderby('buy','asc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                    case 'price':
                        $sort=category::all();
                        $books=DB::table('books')->where('cid',$id)->orderby('price','desc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                }
            }else{
                $sort=category::all();
                $books=book::where('cid',$id)->paginate(8);
                $newsort=$id;
                return view('linfeng.home.sort',compact('books','sort','newsort'));
            }
        }else{
            //在默认显示全部中判断有没有操作
            if ($do){
                //对操作进行判断
                switch ($do){
                    case "scan":
                        $sort=category::all();
                        $books=DB::table('books')->orderby('scan','desc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                    case "netime":
                        $sort=category::all();
//                        $santime=time()-2592000;
//                        dump(date('Y-m-d',$santime));
                        $books=DB::table('books')->orderby('addtime','desc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                    case "buy":
                        $sort=category::all();
                        $books=DB::table('books')->orderby('buy','desc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                    case 'price':
                        $sort=category::all();
                        $books=DB::table('books')->orderby('price','desc')->paginate(8);
                        $newsort=$id;
                        return view('linfeng.home.sort',compact('books','sort','newsort'));
                        break;
                }
            }else{
                //显示没有操作的情况下
                $sort=category::all();
                $books=DB::table('books')->paginate(8);
                $newsort=$id;

                return view('linfeng.home.sort',compact('books','sort','sum','newsort'));
            }
        }
    }

    //榜单
    public function lists($type='buy')
    {
        switch ($type){
            case 'buy':
//                $sort=category::all();
                $books=DB::table('books')->orderby('buy','desc')->paginate(12);
                $name='畅销榜';

                return view('linfeng.home.lists',compact('books','name'));
                break;
            case 'newtime':
                $santime=time()-2592000;
//                        dump(date('Y-m-d',$santime));
                $books=DB::table('books')->where('addtime','>',$santime)->orderby('addtime','desc')->paginate(12);
                $name='新书榜';
                return view('linfeng.home.lists',compact('books','name'));
                break;
            case 'scan':
//                $sort=category::all();
                $books=DB::table('books')->where('scan','>',20)->orderby('scan','desc')->paginate(12);
                $name='点击榜';
                return view('linfeng.home.lists',compact('books','name'));
                break;
        }

    }

    //出版社
    public function press()
    {
        $books='';
        $press=presse::all();
//        dd($press);
        return view('linfeng.home.press',compact('books','press'));
    }

    ///出版社中的子模板
    public function domain()
    {
        $books='';
        return view('linfeng.home.domain',compact('books'));
    }



    //书籍详情信息
    public function message(Request $request,$id)
    {
        //$id是书籍的id

        //对有用户的浏览记录进行增加如果有记录就不进行增加数据库
        if (!empty(session('readid'))){

            $newcollers=readuser::find(session('readid'))->recordbooks()->where('books.id',$id)->get()->toArray();

            if (!count($newcollers)){
                $count=coller::where('readid',session('readid'))->count();
                if (!empty($count>8)){
                    $oldcollers=record::where('readid',session('readid'))->orderby('addtime','asc')->first();
                    $oldcollers->delete();
                }
                $addcoller=new record();
                $addcoller->readid=session('readid');
                $addcoller->bid=$id;
                $addcoller->addtime=time();
                $addcoller->save();
            }

        }

        //对提交的评论进行增加
        if ($request->isMethod('post')){
            $readname=$request->readname;
            $readid=$request->readid;
            $bookname=$request->bookname;
            $bookid=$request->bookid;
            $text=$request->text;

            $add=new comment();
            $add->bid=$bookid;
            $add->rid=$readid;
            $add->bookname=$bookname;
            $add->readname=$readname;
            $add->comment=$text;
            $add->addtime=time();
            $add->save();

            return redirect('home/message/'.$id);

        }
        //点击量
        if (!empty(session('readid'))) {
            $readsacn = scangood::where('bid', $id)->where('rid', session('readid'))->get();
            foreach ($readsacn as $k){
                $oldtime=$k->addtime;
            }
//            dd($oldtime);
            if (!empty($oldtime)) {
                if ((time()-$oldtime) > 10) {
                    //取出当前的点击加上1在送回数据库
                    $scan = book::find($id);
                    $newscan = $scan->scan;
                    $newscan = $newscan + 1;
                    $sumscan = book::find($id);
                    $sumscan->scan = $newscan;
                    $sumscan->save();
                    $newreadsacn = scangood::where('bid', $id)->where('rid', session('readid'))->get();
                    foreach ($newreadsacn as $k){
                      $newreadsacnid=$k->id;
                     }
                    $lllnewreadsacn=scangood::find($newreadsacnid);
                    $lllnewreadsacn->addtime = time();
                    $lllnewreadsacn->save();
                }
            } else {
                $scan = book::find($id);
                $newscan = $scan->scan;
                $newscan = $newscan + 1;
                $sumscan = book::find($id);
                $sumscan->scan = $newscan;
                $sumscan->save();

                $addreadscan = new scangood();
                $addreadscan->rid = session('readid');
                $addreadscan->bid = $id;
                $addreadscan->addtime = time();
                $addreadscan->save();
            }
        }



        $readid=session('readid');


        $readchapter=piddetail::where('readid',$readid)->where('bid',$id)->get();
        if (count($readchapter)){
            foreach($readchapter as $k){
                $books[]=$k->chapter;
            }
            session(['books'=>$books]);
        }


//        dd(session('books'));

//书章
        $chapter=chapter::where('bid',$id)->orderby('zhongshu','asc')->get();
//        dd($chapter);
        //评论
        $comment=comment::where('bid',$id)->get();
        $comment=DB::table('readusers')->join('comments','comments.rid','readusers.id')->where('comments.bid',$id)->get();
//        dd($comment);
//        dd($comment);
        //所有书的书卷
        $rolls=bookroll::where('bid',$id)->get();
//        dd($comment);
        $books='';
        //书籍详情
        $book=book::find($id);

        //判断是否收藏图书
        $nelcolers=coller::where('readid',$readid)->where('bid',$id)->get();

        return view('linfeng.home.message',compact('book','books','rolls','comment','chapter','id','nelcolers'));
    }

    //购买选择
    public function piddetails(Request $request)
    {
        //没有登入不给购买
        if (session('readid')){
            //获得数据
            $bid=$request->bid;
            $rid=$request->rid;
            $chapters[]=$request->zhang;
            $pwd=$request->pwd;
            //判断有没有选择章节
            if(!$request->zhang){
                return back()->with('success','<script>alert("请选择章节")</script>');
            }
            //判断密码
            if ($pwd){
                $readpwd=readuser::find(session('readid'));
//                dd($readpwd->pwd);
                if (Hash::check($request->pwd,$readpwd->pwd)){

                    foreach ($chapters as $k) {
                        foreach ($k as $v) {
                            $newcha[] = $v;
                        }
                    }
                    //先进行判断有没有足够的书币可以购买,
                    $readmoneys=readuser::find($rid);
                    $readmoney=$readmoneys->refill;
                    //书章单价得出买章节的总价
                    $bookmoneys=book::find($bid);
                    $bookmoney=$bookmoneys->price;
                    $price=$bookmoneys->price;
                    $bookmoney=$bookmoney*count($newcha);
                    $bookname=$bookmoneys->bookname;

                    if ($readmoney <= '0'){
                        return back()->with('success','<script>alert("账户书币不足,请充值后再购")</script>');
                    }
                    if ($readmoney<$bookmoney){
                        return back()->with('success','<script>alert("账户书币不足,请充值后再购")</script>');
                    }

                    //查询用户
                    $newread=readuser::find($rid);
                    $newread->refill=$readmoney-$bookmoney;
                    $newread->save();

                   //分割成数组章节上传到订单表
                    $he = implode("章,", $newcha);
                    $neworder=new order();
                    $neworder->readid=$rid;
                    $neworder->bid=$bid;
                    $neworder->num=count($newcha);
                    $neworder->chapterssum=$he;
                    $neworder->bookname=$bookname;
                    $neworder->sum=$bookmoney;
                    $neworder->addtime=time();
                    $neworder->orderson=rand(1000,9000).time();
                    $neworder->danjia=$price;
                    $neworder->save();

                    //在上传作者购买详情
//                    $writerid=book::find($bid)->mywriter()->get();
//                    dd($writerid);
                    $writerid=book::find($bid);
                    $wid=$writerid->wid;
                    $writername=$writerid->writername;
//                    $writer=writer::find($wid);
                    $addtabke=new writer_take();
                    $addtabke->wid=$wid;
                    $addtabke->data=time();
                    $addtabke->bid=$bid;
                    $addtabke->bookin=$bookname;
                    $addtabke->sum=$bookmoney;
                    $addtabke->type=1;
                    $addtabke->save();


                    //添加上作者总收入
                    $addmoney=writer::find($wid);
                    $newmoney=$addmoney->money;
                    $addmoney->money=$newmoney+$bookmoney;
                    $addmoney->save();


                    $chapersumm=count($chapters);
                    foreach ($chapters as $k){
                        foreach($k as $v){
                            $add=new piddetail();
                            $add->readid=$rid;
                            $add->bid=$bid;
                            $add->chapter=$v;
                            $add->addtime=time();
                            $add->save();
                        }
                        return back()->with('success','<script>alert("购买成功,共买'.$chapersumm.'章,共用'.$bookmoney.'数币")</script>');
                    }

                }else{
                    return back()->with('success','<script>alert("密码不正确")</script>');
                }
            }else{
                return back()->with('success','<script>alert("密码为空")</script>');
            }

        }else{
//            dd(11);
            return back()->with('success','<script>alert("请先登入")</script>');
        }

    }


    //用户收藏书籍ajax
    public function collers(Request $request)
    {
        $bid=$request->bid;

        $oldcollers=coller::where('bid',$bid)->where('readid',session('readid'))->get();
        if (!count($oldcollers)){
            $addcollers=new coller();
            $addcollers->bid=$bid;
            $addcollers->readid=session('readid');
            $addcollers->addtime=time();
            $addcollers->save();
            return 111;
        }else{


        }


    }


    //小说下载免费章节
    public function ingo($id){
        //获得上传的正文
        //获得书籍详情
        $book=book::find($id);

        $zhong=chapter::where('bid',$id)->orderby('zhongshu','asc')->get();
        $text='';
        foreach($zhong as $k){
            if($book->free>=$k->zhongshu){
                $text.='第'.$k->zhongshu.'章'.$k->site.'<br>';
                continue;
            }elseif($book->free=='all'){
                $text.='第'.$k->zhongshu.'章'.$k->site.'<br>';
            }
        }

        //转换上次中文名
        $bookname=$book->bookname;
        $bookname=iconv("utf-8","gb2312", $bookname);
        $dir=base_path().'/text/'.$bookname.'.txt';
        //保存文件
        file_put_contents($dir,$text);

        //文件下载
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($dir));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($dir));
        ob_clean();
        flush();
        readfile($dir);
    }


    //书籍详情的章节ajax
    public function chapters(Request $request)
    {
        $rid=$request->rid;
        $sum=chapter::where('rid',$rid)->get();
        echo json_encode($sum);
    }


    //书籍评论点赞ajax
    public function comment(Request $request)
    {
        $bid=$request->bid;
        $rid=$request->rid;
        $cid=$request->cid;

        $goodnew=commentsgood::where('bid',$bid)->where('rid',$rid)->where('cid',$cid)->get();
        if (!count($goodnew)){

            $add=new commentsgood();
            $add->rid=$rid;
            $add->bid=$bid;
            $add->cid=$cid;
            $add->save();

            //先拿到值
            $add=comment::find($cid);
            $sum=$add->good;
            $sum=$sum+1;

            //再重新送值
            $good=comment::find($cid);
            $good->good=$sum;
            $good->save();

        }else{
            echo 1111;
        }

    }


    //书籍正文(包括这个方式访问正文,和免费章节和判断是否有)
    public function article($id,$do=0,$newtext=0)
    {
        //$id是书籍的id
        //$do用来进行判断显示的章节,没有$do传入默认显示第一章节
        //newtext是当前的章数
        //判断是否有购买章节
        //还是免费章节
        //readid是读者id原来进行判断有没有购买章节的
        $booknames=book::find($id);
//        $booknames=book::where('id',$id)->get();
//        $booknames=DB::table('chats')->where('id',$id)->get();
//        foreach($booknames as $k){
//            $bookname=$k->bookname;
//            $writername=$k->writername;
//            $cid=$k->cid;
//        }
//        dd($booknames);
        $readid=session('readid');
        $bookname=$booknames->bookname;
        $writername=$booknames->writername;
        $cid=$booknames->cid;
        $sum=DB::table('chapters')->where('bid',$id)->get();
        $mm=array();
        foreach($sum as $k){
            $mm[]=$k->zhongshu;
        }
        if (!empty($mm)){
            $max=max($mm);
        }
        if (!empty($mm)){
            $min=min($mm)+1;
        }
        if(!$newtext){
            $rolls=bookroll::where('bid',$id)->get();
            $chapter=DB::table('chapters')->where('bid',$id)->get();
            //$newtext等于0
            $alert='';
            $newtext=1;
            //通过对购买章节表的查询来判断可以不可以看或者是否有免费章节全部免费是all
            if($booknames->free=='all'){
                $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
            }
            if ($booknames->free>=$newtext){
                $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
            }else{
                $goodzhong=piddetail::where('readid',$readid)->where('bid',$id)->get();
                $gochapter=array();
                foreach($goodzhong as $k){
                    $gochapter[]=$k->chapter;
                }
                if($booknames->free=='all'){
                    $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                    return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                }
                if (in_array($newtext,$gochapter)){
                    $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                    return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                }else{
                    $free='请购买此章节';
                    $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                    return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid','free'));
                }
            }
        }else{
            if($do){
                //$newtext对于0
                if($do=='next'){
                    //判断最大不能大于max
                    if ($max>$newtext){
                        $rolls=bookroll::where('bid',$id)->get();
                        $chapter=DB::table('chapters')->where('bid',$id)->get();
                        $alert='';
                        $newtext=$newtext+1;
                        if ($booknames->free>=$newtext) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                        }else{
                            $goodzhong=piddetail::where('readid',$readid)->where('bid',$id)->get();
                            $gochapter=array();
                            foreach($goodzhong as $k){
                                $gochapter[]=$k->chapter;
                            }
                            if($booknames->free=='all'){
                                $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                                return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                            }
                            if (in_array($newtext,$gochapter)){
                                $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                                return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                            }else {

                                $free = '请购买此章节';
                                $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                                return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid', 'free'));
                            }
                        }
                    }else{
                        $rolls=bookroll::where('bid',$id)->get();
                        $chapter=DB::table('chapters')->where('bid',$id)->get();
                        $alert='<script>alert("这是最后一章了,请等待作者更新")</script>';
                        if ($booknames->free>=$newtext) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                        }else {
                            $goodzhong = piddetail::where('readid', $readid)->where('bid', $id)->get();
                            $gochapter = array();
                            foreach ($goodzhong as $k) {
                                $gochapter[] = $k->chapter;
                            }
                            if($booknames->free=='all'){
                                $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                                return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                            }
                            if (in_array($newtext, $gochapter)) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                            } else {
                            $free = '请购买此章节';
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid', 'free'));
                        }
                        }
                    }
                }elseif ($do=='top') {
                    //判断如果小于最下章节就返回最小章节
                    if ($min > $newtext) {
                        $rolls=bookroll::where('bid',$id)->get();
                        $chapter=DB::table('chapters')->where('bid',$id)->get();
                        $alert='<script>alert("这是第一章")</script>';
                        if ($booknames->free>=$newtext) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                        }else{
                            $goodzhong = piddetail::where('readid', $readid)->where('bid', $id)->get();
                            $gochapter = array();
                            foreach ($goodzhong as $k) {
                                $gochapter[] = $k->chapter;
                            }
                            if($booknames->free=='all'){
                                $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                                return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                            }
                            if (in_array($newtext, $gochapter)) {
                                $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                                return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                            }else {
                                $free = '请购买此章节';
                                $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                                return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid', 'free'));
                            }
                        }
                    } else {
                        $rolls=bookroll::where('bid',$id)->get();
                        $chapter=DB::table('chapters')->where('bid',$id)->get();
                        $alert='';
                        $newtext = $newtext - 1;
                        if ($booknames->free>=$newtext) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                        }else{
                            $goodzhong = piddetail::where('readid', $readid)->where('bid', $id)->get();
                            $gochapter = array();
                            foreach ($goodzhong as $k) {
                                $gochapter[] = $k->chapter;
                            }
                            if($booknames->free=='all'){
                                $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                                return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                            }
                            if (in_array($newtext, $gochapter)) {
                                $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                                return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                            }else {
                                $free = '请购买此章节';
                                $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                                return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid', 'free'));
                            }
                        }
                    }
                }
            }else{
                //直接使用书id和章节id进行访问
                if ($newtext){
                    $rolls=bookroll::where('bid',$id)->get();
                    $chapter=DB::table('chapters')->where('bid',$id)->get();
                    $alert='';
                    if ($booknames->free>=$newtext) {
                        $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                        return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                    }else{
                        $goodzhong = piddetail::where('readid', $readid)->where('bid', $id)->get();
                        $gochapter = array();
                        foreach ($goodzhong as $k) {
                            $gochapter[] = $k->chapter;
                        }
                        if($booknames->free=='all'){
                            $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                            return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                        }
                        if (in_array($newtext, $gochapter)) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                        }else {
                            $free = '请购买此章节';
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', $newtext)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid', 'free'));
                        }
                    }
                }else{
                    $rolls=bookroll::where('bid',$id)->get();
                    $chapter=DB::table('chapters')->where('bid',$id)->get();
                    $alert='';
                    if ($booknames->free>=$newtext) {
                        $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', 1)->get();
                        return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                    }else{
                        $goodzhong = piddetail::where('readid', $readid)->where('bid', $id)->get();
                        $gochapter = array();
                        foreach ($goodzhong as $k) {
                            $gochapter[] = $k->chapter;
                        }
                        if($booknames->free=='all'){
                            $text=DB::table('chapters')->where('bid',$id)->where('zhongshu',$newtext)->get();
                            return view('linfeng.home.article',compact('text','id','alert','chapter','rolls','bookname','writername','cid'));
                        }
                        if (in_array($newtext, $gochapter)) {
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', 1)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid'));
                        }else {
                            $free = '请购买此章节';
                            $text = DB::table('chapters')->where('bid', $id)->where('zhongshu', 1)->get();
                            return view('linfeng.home.article', compact('text', 'id', 'alert', 'chapter', 'rolls', 'bookname', 'writername', 'cid', 'free'));
                        }
                    }
                }
            }
        }
    }


    //正文的背景颜色ajax
    public function color(Request $request)
    {
        $color=$_GET['color'];
        $read=readuser::find(session('readid'));
        $read->backgroud=$color;
        $a=$read->save();
        dd($a);
        if (!$a){
            echo 222;
        }
//        echo 11;
    }

    //正文的字体大小ajax
    public function size(Request $request)
    {
        $size=$request->size;
        $read=readuser::find(session('readid'));
        $read->ziti=$size;
        $c=$read->save();
        if (!$c){
            echo 222;
        }
    }

    //正文左侧列表章节ajax
    public function articlelist(Request $request)
    {
        $bid=$request->bid;
        $rid=$request->rid;
//        $sum=DB::table('chapters')->where('bid',$bid)->where('rid',$rid)->orderby('chapterssum','desc')->get();
        $sum=chapter::where('bid',$bid)->orderby('zhongshu','desc')->get();
        echo json_encode($sum);
    }

    //书籍正文的聊天室ajax
    public function chat(Request $request){

        if (!$request->isMethod('post')){
            $bid=$request->bid;
//                $new=DB::table('chats')->where('bid',$bid)->orderby('addtime','desc')->where('show','1')->get();
            if ($request->newchatid) {
                $mixid=$request->mixid;
                $newtime=time()-2;
                if ($mixid){
                    $new = chat::where('bid', $bid)->orderby('addtime', 'asc')->where('id' , '>', $mixid)->get();
                    echo json_encode($new);
                }else{
                    $newtime=time()-5;
                    $new = chat::where('bid', $bid)->orderby('addtime', 'asc')->where('addtime' , '>', $newtime)->get();
                    echo json_encode($new);
                }
            }else{
                $newtime=time()-1;
                $new = chat::where('bid', $bid)->orderby('addtime', 'asc')->where('addtime' , '>', $newtime)->get();
                echo json_encode($new);
            }
        }else{
            $bid=$request->bid;
            $bookname=$request->bookname;
            $rid=$request->rid;
            $readname=$request->readname;
            $text=$request->text;
//        str_replace("\n","<br>",$str);
            $add=new chat();
            $add->bid=$bid;
            $add->rid=$rid;
            $add->readname=$readname;
            $add->bookname=$bookname;
            $add->addtime=time();
            $add->text=$text;
            $add->show=1;
            $add->date= date("Y-m-d H:i:s",time());
            $add->save();
            $newchatid=$add->id;
            echo $newchatid;
        }
    }

    //实现刷新聊天框ajax
    public function chashow(Request $request)
    {
        $bid=$request->bid;
//                $new=DB::table('chats')->where('bid',$bid)->orderby('addtime','desc')->where('show','1')->get();

        $new=chat::where('bid',$bid)->orderby('addtime','desc')->where('show',1)->get();
        echo json_encode($new);
        foreach($new as $k){
//                    dump($k->id);
//                    DB::table('chats')->where('id',$k->id)->update('show',2);
            DB::table('chats')->where('id',$k->id)->update(
                ['show'=>'2']
            );
        }
    }


    //退出
    public function out(Request $request){
//        $request->session()->forget('readid');
//        $request->session()->forget('readtel');
//        $request->session()->forget('readname');
        $request->session()->flush();
        return redirect('home/index');
    }

    //个人中心
    public function person()
    {

        $collers=DB::table('books')->leftjoin('collers','collers.bid','books.id')->where('collers.readid',session('readid'))->paginate(8);

        if (!empty(count($collers))){
            $books='';
            $read=readuser::find(session('readid'));
            $collers=DB::table('books')->leftjoin('collers','collers.bid','books.id')->where('collers.readid',session('readid'))->paginate(8);

            return view('linfeng.home.person',compact('books','read','collers'));
        }
        $books='';
//        dd($collers);
        $read=readuser::find(session('readid'));
        return view('linfeng.home.person',compact('books','read','collers'));
    }

    //完善信息
    public function perfect()
    {
        $read=readuser::find(session('readid'));
        $books='';
        return view('linfeng.home.perfect',compact('books','read'));
    }

    //修改信息
    public function person_edit(Request $request){
        if ($request->isMethod('post')){

        }
        $books='';
        if (!empty(session('readid'))){
            $name=readuser::find(session('readid'));
//            dd(session('readid'));
            $read=readuser::find(session('readid'));
            return view('linfeng.home.person_edit',compact('books','name','read'));
        }

    }

    //登陆历史
    public function history(){
        $books='';
        $read=readuser::find(session('readid'));
        return view('linfeng.home.history',compact('books','read'));
    }

    //打赏作者
    public function dashang(Request $request){

        //在上传作者购买详情
        $book=book::find($request->bid);
        $bookname=$book->bookname;
        $wid=$book->wid;

        $addmoney=writer::find($wid);
        $newmoney=$addmoney->money;
        $addmoney->money=$newmoney+1;
        $addmoney->save();

        $addtabke=new writer_take();
        $addtabke->wid=$wid;
        $addtabke->data=time();
        $addtabke->bid=$request->bid;
        $addtabke->bookin=$bookname;
        $addtabke->sum=1;
        $addtabke->type=2;
        $addtabke->save();
        if ($addtabke){
            return 111;
        }else{
            return 222;
        }
    }


}
